# -*- coding: utf-8 -*-

import sip
sip.setapi('QString', 2)
sip.setapi('QStringList', 2)
sip.setapi('QVariant', 2)

from PyQt4 import QtCore

#_______________________________________________________________________________
class SongLabelController(QtCore.QObject):   
    def __init__(self, server, view):
        QtCore.QObject.__init__(self)
        self.server_ctrl = server
        self.server_model = server.model
        self.view = view
        #Signal
        self.server_ctrl.client_connected.connect(self.setup)
        self.server_ctrl.player_song_changed.connect(self.update_information)
        self.server_ctrl.player_started.connect(self.update_information)
        self.server_ctrl.player_stopped.connect(self.reset_information)
    
    def setup(self, connected):
        if connected is True:
            if self.server_model.info_status["state"] != "stop":
                self.update_information()
            else:
                self.reset_information()
        else:
            pass
    
    def reset_information(self):
#        self.view.update_label()
#        self.view.reset_label()
        text = self.view.default_text 
        self.view.setText(text.decode("utf-8"))
    
    def update_information(self):
        text = []
        title = self.server_model.info_song["title"]
        if title == None:
            title = "unknown"
        item = self.view.text_format[0] % str(title)
        text.append(item.decode("utf-8"))
        artist = self.server_model.info_song["artist"]
        if artist == None:
            artist = "unknown"
        item = self.view.text_format[1] % str(artist)
        text.append(item.decode("utf-8"))
        album = self.server_model.info_song["album"]
        if album == None:
            album = "unknown"
        item = self.view.text_format[2] % str(album)
        text.append(item.decode("utf-8"))
        year = self.server_model.info_song["date"]
        if year == None:
            year = ""
        item = self.view.text_format[3] % str(year)
        text.append(item.decode("utf-8"))
#        self.view.update_label(data)
        text = "".join(text)
        self.view.setText(text)